//
//  LoginViewController.swift
//  FastDevelop
//
//  Created by IOS1 on 2025/6/26.
//

import UIKit

class WelcomeController: BaseViewController, UICollectionViewDelegate, UICollectionViewDataSource {

    private var dataSource: [LoginItem] = [.one, .two, .three]
    
    override func viewDidLoad() {
        super.viewDidLoad()

        view.addSubviews([
            skipButton,
            collectionView,
            nextButton
        ])
        
        skipButton.snp.makeConstraints { make in
            make.trailing.equalToSuperview()
            make.top.equalToSuperview().offset(CGFloat.statusHeight)
            make.height.equalTo(44)
            make.width.equalTo(130)
        }
        collectionView.snp.makeConstraints { make in
            make.leading.equalToSuperview()
            make.top.equalTo(skipButton.snp.bottom).offset(100)
            make.height.equalTo(440)
            make.width.equalTo(CGFloat.screenWidth)
        }
        nextButton.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(collectionView.snp.bottom).offset(54)
            make.width.equalTo(325)
            make.height.equalTo(56)
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dataSource.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "WelcomeBannarCell", for: indexPath) as? WelcomeBannarCell else { return UICollectionViewCell() }
        cell.model = dataSource[indexPath.row]
        return cell
    }
    
    private lazy var skipButton: UIButton = {
        let v = UIButton()
        v.setTitle("überspringen", for: .normal)
        v.setTitleColor(UIColor(hexString: "#0E84FF"), for: .normal)
        v.titleLabel?.font = UIFont.systemFont(ofSize: 18)
        v.addTarget(self, action: #selector(clickSkipButton), for: .touchUpInside)
        return v
    }()

    private lazy var collectionView: UICollectionView = {
        let v = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())
        
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.itemSize = CGSize(width: UIScreen.main.bounds.width, height: 440)
        layout.scrollDirection = .horizontal
        
        v.collectionViewLayout = layout
        v.backgroundColor = .clear
        v.register(WelcomeBannarCell.self, forCellWithReuseIdentifier: "WelcomeBannarCell")
        v.showsHorizontalScrollIndicator = false
        v.delegate = self
        v.dataSource = self
        v.isPagingEnabled = true
        
        return v
    }()
    
    private lazy var nextButton: UIButton = {
        let v = UIButton()
        v.setImage(UIImage(named: "continue"), for: .normal)
        v.addTarget(self, action: #selector(clickContinueButton), for: .touchUpInside)
        return v
    }()
}

//MARK: action
extension WelcomeController {
    
    @objc private func clickContinueButton(){
        
        guard let firstVisibleIndexPath = collectionView.indexPathsForVisibleItems.min() else { return }
               
            let nextItem = firstVisibleIndexPath.item + 1
               
            if nextItem < dataSource.count {
                let nextIndexPath = IndexPath(item: nextItem, section: firstVisibleIndexPath.section)
                collectionView.scrollToItem(at: nextIndexPath, at: .top, animated: true)
            } else {
                
                UserDefaultsManager.setBool(true, forKey: .first)
                UIApplication.window?.rootViewController = BaseTabBarVC()
            }
    }
    
    @objc private func clickSkipButton(){
        
        UserDefaultsManager.setBool(true, forKey: .first)
        UIApplication.window?.rootViewController = BaseTabBarVC()
    }
}


class WelcomeBannarCell: UICollectionViewCell {

    private lazy var iconImageV: UIImageView = {
        let v = UIImageView()
        v.contentMode = .scaleAspectFill
        return v
    }()
    private lazy var controlImageV: UIImageView = {
        let v = UIImageView()
        v.contentMode = .scaleAspectFill
        return v
    }()
    
    var model: LoginItem? {
        didSet{
            guard let md = model else { return }
            
            iconImageV.image = UIImage(named: md.icon)
            controlImageV.image = UIImage(named: md.control)
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        contentView.backgroundColor = .clear
        backgroundColor = .clear
        
        contentView.addSubviews([
            iconImageV,
            controlImageV
        ])
        
        iconImageV.snp.makeConstraints { make in
            make.top.centerX.equalToSuperview()
            make.width.height.equalTo(300)
        }
        controlImageV.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(iconImageV.snp.bottom).offset(120)
            make.width.equalTo(75)
            make.height.equalTo(20)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
